﻿using System.Collections;
using System.Collections.Generic;
using MotionFramework;
using MotionFramework.AI;
using UnityEngine;
using YooAsset;

internal class FsmUpdateStaticVersion : IFsmNode
{
	public string Name { private set; get; } = nameof(FsmUpdateStaticVersion);

	void IFsmNode.OnEnter()
	{
		Debug.Log("更新资源版本号");

		PatchEventDispatcher.SendPatchStepsChangeMsg(EPatchStates.UpdateStaticVersion);
		MotionEngine.StartCoroutine(GetStaticVersion());
	}

	void IFsmNode.OnUpdate()
	{
	}

    void IFsmNode.OnFixedUpdate()
    {
    }

    void IFsmNode.OnHandleMessage(object msg)
    {
    }
	
	void IFsmNode.OnExit()
	{
	}

	private IEnumerator GetStaticVersion()
	{
		yield return new WaitForSecondsRealtime(0.5f);

		// 更新资源版本号
		var operation = YooAssets.UpdateStaticVersionAsync(30);
		yield return operation;

		if (operation.Status == EOperationStatus.Succeed)
		{
			Debug.Log($"Found static version : {operation.ResourceVersion}");
			PatchUpdater.ResourceVersion = operation.ResourceVersion;
			PatchUpdater.Fsm.Transition(nameof(FsmUpdateManifest));
		}
		else
		{
			Debug.LogWarning(operation.Error);
			PatchEventDispatcher.SendStaticVersionUpdateFailedMsg();
		}
	}
}